package test;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.bean.User;
import org.example.mapper.UserMapper;
import org.example.util.SqlSessionUtil;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TT01 {


    @Test
    public void testInsert() throws IOException {
        /*InputStream is = Resources.getResourceAsStream("Mybatis.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
       //openSession(true)表示自动提交事务，就可以不用手动提交事务
        SqlSession sqlSession = sqlSessionFactory.openSession(true);*/

        SqlSession sqlSession = SqlSessionUtil.getSqlSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int i = userMapper.insertUser();
        System.out.println(i);

        sqlSession.close();

    }
    //修改
    @Test
    public void testUpdate() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int i = userMapper.updateUser();
        System.out.println(i);

        sqlSession.close();

    }

    /**
     * 根据id删除
     */
    @Test
    public void removeById(){

        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.removeById();
        System.out.println(i);
        sqlSession.close();

    }
    /**
     * 根据id查询某一个数据
     */
    @Test
    public void selectById(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectById();
        System.out.println(user);
        sqlSession.close();
    }
    /**
     * 查询全部数据
     */
    @Test
    public void selectAll(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.selectAll();
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }



}
